/*
 * clock.c
 *
 *  Created on: Jun 3, 2014
 *      Author: Administrator
 */
#include "../bsp/bsp.h"
#include "clock.h"
#ifdef __STM32
    #include "../bsp/arch/stm32/stm32f1xx/STM32F10x_StdPeriph_Driver/inc/stm32f10x_iwdg.h"
#endif
void MCU_Clock_Init(void)
{
#ifdef __R8C
    uint8 i = 0;

    /*--------------------------------------------------
    -	Change on-chip oscillator clock to Main clock  -
    ---------------------------------------------------*/
    prc0 = 1; /* Protect off                                          */
    cm13 = 1; /* Xin Xout                                             */
    cm15 = 1; /* XIN-XOUT drive capacity select bit : HIGH            */
    cm05 = 0; /* Xin on                                               */
    cm16 = 0; /* Main clock = No division mode                        */
    cm17 = 0;
    cm06 = 0; /* CM16 and CM17 enable                                 */
    cm07 = 0; /* CM16 and CM17 enable                                 */
    while (i <= 100) /* This setting is an example of waiting time for the   */
        i++;        /* oscillation stabilization. Please evaluate the time  */
    /* for oscillation stabilization by a user.             */
    ocd2 = 0; /* Main clock change                                    */
    prc0 = 0; /* Protect on                                           */
#endif
}

void wdt_init(void)
{
#ifdef __R8C
    prc1 = 1; //PM12保护取消
    pm12 = 1; //溢出重启mcu
    prc1 = 0;
    cspro = 0;
    wdc7 = 1; //128分频,38ms,溢出一次
    wdtr = 0;
    wdtr = 0xFF;
    wdts = 1;
#endif
#ifdef __STM32
//0.8毫秒计数一次，内部看门狗使用内部的40KHz的时钟。
    IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
    IWDG_SetPrescaler(IWDG_Prescaler_32);
    IWDG_SetReload(125); //100ms复位
    IWDG_WriteAccessCmd(IWDG_WriteAccess_Disable);
    IWDG_Enable();
#endif
}
